package com.example.fp.zlz.controller;

import com.example.fp.cgc.code.logAnno.LogAnno;
import com.example.fp.cgc.entity.PageCondition;
import com.example.fp.zlz.entity.Game;
import com.example.fp.zlz.entity.PageConditionGame;
import com.example.fp.zlz.mapper.GameMapper;
import com.example.fp.zlz.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping
public class GameWebController {
    @Autowired
    private GameMapper gameMapper;

    String search;
    @RequestMapping(value = "/getGamesByName/{page}")
    public ModelAndView getgameListWeb( @PathVariable("page") int page,
                                        @RequestParam(required = false, defaultValue = "") String titlename,
                                        Model model){
        int count = 0;

        if(!titlename.isEmpty() && !"null".equals(titlename)) {
            model.addAttribute("gname", titlename);
            search = titlename;
            model.addAttribute("titlename", titlename);
            count=gameMapper.getGameByName(titlename);
        } else {
            model.addAttribute("titlename", titlename);
            count=gameMapper.selectgamesum();
        }
        if (count == 0) {
            model.addAttribute("count", count);
            return new ModelAndView("gameweb/gamelist", "gameModel", model);
        } else {
            int pagesize = 10;
            PageUtil.Page pageData = PageUtil.getPage(page, count, pagesize);
            if (!titlename.isEmpty() && !"null".equals(titlename)) {
                PageConditionGame pc = new PageConditionGame(pageData.currLoc, pagesize, titlename);
                List<Game> gameList = gameMapper.selectSearchGamePage(pc);
                model.addAttribute("gameList", gameList);
            } else {
                PageConditionGame pc = new PageConditionGame(pageData.currLoc, pagesize, "");
                List<Game> gameList = gameMapper.selectGameListPage(pc);
                model.addAttribute("gameList", gameList);
            }


            model.addAttribute("pageNo", page);
            model.addAttribute("count", count);
            model.addAttribute("maxpage",pageData.maxPage);
            model.addAttribute("currMaxPage", pageData.currMaxPage);

            return new ModelAndView("gameweb/gamelist", "gameModel", model);
        }
    }
    @LogAnno(operateType = "查询当前游戏列表")
    @RequestMapping(value = "/gamelist/{page}")
    public ModelAndView getallGame(@PathVariable("page") int page, Model model) {
        if (page == 0) {
            page = 1;
        }
        int pagesize = 10;
        int count = gameMapper.selectgamesum();
        if (count == 0) {
            model.addAttribute("count", count);
            return new ModelAndView("gameweb/gamelist", "gameModel", model);
        } else {
            int maxpage = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
            if (page > maxpage) {
                page = maxpage;
            }
            int temp = (page - 1) * pagesize;
            PageConditionGame pc = new PageConditionGame(temp, pagesize, "");
            List<Game> gameListbypage = gameMapper.selectGameListPage(pc);

            int currMaxPage = Math.min(page + 10, maxpage);

            model.addAttribute("gameList", gameListbypage);
            model.addAttribute("pageNo", page);
            model.addAttribute("count", count);
            model.addAttribute("maxpage", maxpage);
            model.addAttribute("currMaxPage", currMaxPage);

            return new ModelAndView("gameweb/gamelist", "gameModel", model);
        }
    }

    @RequestMapping(value = "/gamesuccess")
    public ModelAndView succGame(Model model) {
        return new ModelAndView("gameweb/success", "gameModel", model);
    }

    @GetMapping(value = "/gameForm")
    public ModelAndView createGameForm(Model model) {
        model.addAttribute("game", new Game());
        model.addAttribute("title", "创建一个新的游戏");
        return new ModelAndView("gameweb/addgame", "gameModel", model);
    }


    @PostMapping(value = "/addOrModityGame")
    public ModelAndView saveOrUpdateGame(Game game) {
        Game game1 = gameMapper.selectById(game.getId());

        if (game1 == null) {
            gameMapper.insert(game);
            return new ModelAndView("redirect:/success");//重定向到list页面
        } else {
            String titlename=search;
            int count = gameMapper.getGameIndex(game.getId());
            int i = count % 10;
            int page = count / 10;
            if (i > 0) {
                page += 1;
            }
            gameMapper.updateById(game);
            return new ModelAndView("redirect:/gamelist/" + page +"?titlename="+titlename); //重定向到list页面
        }
    }


    @GetMapping("/deleteGame/{id}&{page}&{titlename}")
    public ModelAndView deleteGame(@PathVariable("id") int id, @PathVariable("page") int page,
                                   @RequestParam(required = false, defaultValue = "") String titlename) {
        gameMapper.deleteById(id);
        return new ModelAndView("redirect:/gamelist/" + page +"?titlename="+titlename);//重定向到list页面
    }

    @GetMapping("/delSelected/{checkedID}&{page}&{titlename}")
    public ModelAndView delSelected(@PathVariable("checkedID") String checkedID, @PathVariable("page") int page,
                                    @RequestParam(required = false, defaultValue = "") String titlename) {
        String[] ids = checkedID.split(",");
        for (int i = 0; i < ids.length; i++) {
            gameMapper.deleteById(Integer.parseInt(ids[i]));
        }
        return new ModelAndView("redirect:/gamelist/" + page +"?titlename="+titlename);//重定向到list页面
    }


    @GetMapping("/modifyGame/{id}&{titlename}")
    public ModelAndView modifyGame(@PathVariable("id") int id, Model model,
                                   @PathVariable("titlename") String titlename) {
        search = titlename;
        model.addAttribute("game", gameMapper.selectById(id));
        model.addAttribute("title", "修改游戏");
        model.addAttribute("titlename", titlename);
        return new ModelAndView("gameweb/modifyGame", "gameModel", model);
    }


}
